/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.axiom.persistence;

import com.axiom.controller.student_info_controller;
import com.axiom.utility.IdGenerator;
import com.axiom.vo.student_detail;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Shuhail
 */
public class StudentInfo_Persist {
    
    Session sess = null;
    Transaction tx = null;
    
    public static StudentInfo_Persist getInstance() {
        
        return new StudentInfo_Persist();
    }

    public StudentInfo_Persist() {
    
         SessionFactory sf = new Configuration().configure().buildSessionFactory();
         sess = sf.openSession();
         tx = sess.beginTransaction();
    }
    
    
    public boolean insertFresh(student_detail detail) {
        try {
            detail.setStud_id(IdGenerator.getInstance().getValue(this.getTheLastPK()));
            sess.save(detail);
            tx.commit();
        }
        catch(Exception e) {
            return false;
        }
        return true;
    }
    
    
    public student_detail sampleInsertData() {
        
        student_detail detail = new student_detail();
        //////detail.setStud_id("S2");
        detail.setmName("Illa");
        detail.setfName("Shamu");
        detail.setlName("Kadavath");
        detail.setSex("Male");
        detail.setAddressLine1("Kadavath house mysore toad");
        detail.setAddressLine2("Manathavady");
        detail.setState("Kerala");
        detail.setNationality("Indian");
        detail.setPin("670645");
        return detail;
    }
    
    
    public String getTheLastPK() {
        
        List list = this.getAllData();
        List<String> pkList = new ArrayList<String>();
        Iterator it = list.iterator();
        while(it.hasNext()) {
            
            student_detail detail = (student_detail)it.next();
            pkList.add(detail.getStud_id());
        }
        String pk = IdGenerator.getInstance().getTheLastPK(pkList);
        return pk;
    }
    
    public List getAllData() {
        Query query = sess.getNamedQuery("getTheLastPKForStudentInfo");
        List list = query.list();
        return list;
        
       
    }
    
    public int deleteRecord(String stud_id) {
        
        Query query = sess.getNamedQuery("deleteStudent");
        query.setString("stud_id", stud_id);
        try {
            
           int del = query.executeUpdate();
           tx.commit();
           return del;
        }
        catch(Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    
    
    public ScrollableResults getStudentFeeDetails(String name) {
        
        Query query = sess.getNamedQuery("getStudentFeeDetails");
        query.setString("name", name );
        
        ScrollableResults res = query.scroll();
       
        return res;
    }
    
    
        public ScrollableResults getStudentFeeDetailsBasedonId(String name) {
        
        Query query = sess.getNamedQuery("getStudentFeeDetailsBasedOnId");
        query.setString("stud_id", name );
       
        System.out.println(query.getQueryString());
        ScrollableResults res = query.scroll();
        
        return res;
    }
    public ScrollableResults getNameCourseAndPhone() {
        
        
        Query query = sess.getNamedQuery("getStudCourseAndPhone");
        ScrollableResults res = query.scroll(ScrollMode.SCROLL_INSENSITIVE);
       
       
        
        return res;
    }
    
    public static void  main(String[] args) {
        //student_detail det = new StudentInfo_Persist().sampleInsertData();
        //new StudentInfo_Persist().insertFresh(det);
        StudentInfo_Persist sip = StudentInfo_Persist.getInstance();
        System.out.println(sip.getStudentFeeDetailsBasedonId("S1"));
        
        
       
       
    }
}
